package toDoList.Controllers;

import java.io.*;
import java.util.*;

import toDoList.Manager.DataBaseHelper;
import toDoList.Manager.R;
import toDoList.Manager.R.array;
import toDoList.Manager.R.id;
import toDoList.Manager.R.layout;
import toDoList.Models.CategoryModel;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class CategoryController extends Activity implements ControllerInterface {
	
	/** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addcategory);
        updateSpinner();
    }
	
	/**
	 * updateSpinner() Updates the spinners in the "New Category" view
	 */
	private void updateSpinner()
	{
		 //Update the spinner with the strings of colors
		 Spinner recSpinner = (Spinner)findViewById(R.id.addCategoryColorSpinner);
		 ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
		            this, R.array.color_array, android.R.layout.simple_spinner_item);
		 recSpinner.setAdapter(adapter2);
	}
	
	/**
	 * okPressed(View view) Runs when the ok button is pressed. Adds a new category.
	 * @param view The current view on the screen
	 */
	public void okPressed(View view)
	{
		boolean filled = true;
		String errors = "";
		
		if(((EditText)findViewById(R.id.addCategoryNameText)).getText().toString().equals(""))
		{
			errors += "Name must be filled in.";
			filled = false;
		}
		if(((EditText)findViewById(R.id.addCategoryDescriptionText)).getText().toString().equals(""))
		{
			errors += "Description must be filled in.";
			filled = false;
		}
		
		if(filled)
		{
			DataBaseHelper helper = new DataBaseHelper(this);
			SQLiteDatabase db = helper.getWritableDatabase();	
	 		db.execSQL("INSERT INTO Category (name, description, color, catGUID) " + "VALUES (" + "'"+ ((EditText)findViewById(R.id.addCategoryNameText)).getText().toString() + "'" + ", " + "'"+ ((EditText)findViewById(R.id.addCategoryDescriptionText)).getText().toString() + "'" + "," + "'" + ((Spinner)findViewById(R.id.addCategoryColorSpinner)).getSelectedItem().toString().toLowerCase() + "', '" + UUID.randomUUID() + "')");
	 		db.close();
	 		
	 		Cursor c = getCategoryByName(((EditText)findViewById(R.id.addCategoryNameText)).getText().toString());
		}
		else
		{
			popUp(errors);
		}
		
		finish();
	}
	
	/**
	 * getCategoryByName(String name) Gets the category details from the database using the category name
	 * @param name The name to get
	 * @return A cursor containing the rows of data with the category details
	 */
	private Cursor getCategoryByName(String name) {
		
		DataBaseHelper helper = new DataBaseHelper(this);
		SQLiteDatabase db = helper.getReadableDatabase();
			
		    Cursor mCursor =		
		      db.query(true, "Category", new String[] {"name",
		          "description", "color"}, "name" + " = " + "'" + name + "'", null,
		          null, null, null, null);
		    if (mCursor != null) {
		      mCursor.moveToFirst();
		    }
		    db.close();
		    return mCursor;
	}	
	
	/**
	 * cancelPressed(View view) Cancels this view. Reverts back to the previous view on the stack.
	 * @param view The current view on the screen
	 */
	public void cancelPressed(View view)
	{
	    finish();
	}

	/**
	 * createListener() Ties all of the controllers together
	 */
	public void createListener()
	{
		
	}

	/**
	 * popUp(String n) Displays a pop-up window with the message "n" inside of it.
	 * @param n The string to display
	 */
	public void popUp(String n) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(n)
		       .setCancelable(false)
		       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });
		
		builder.show();
	}

}
